/*
 * FrontEndApp.java
 *
 * Created on December 1, 2011, 4:07 PM
 */
 
package com.mabi.frontend;           

import com.mabi.api.appbuilder.AppBuilder;
import com.mabi.api.application.IFrontEndApp;
import com.vaadin.Application;
import com.vaadin.ui.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
/** 
 *
 * @author User
 * @version 
 */
@SessionScoped
public class FrontEndApp extends Application implements IFrontEndApp {
    
    private Properties config;

    @Override
    public void init() {
        try {
            //Loads the configuration
            loadConfig();
            
            //Present default window configuration just in case all else fails
            Window mainWindow = new Window(config.getProperty("DEFAULT_WINDOW_CAPTION"));
            
            //Bulids the application initial state
            AppBuilder appBuilder = getAppBuilder();
            appBuilder.buildApplication(this);
            
            
            Label label = new Label("Hello Vaadin user");
            mainWindow.addComponent(label);
            setMainWindow(mainWindow);
        } catch (IOException ex) {
            Logger.getLogger(FrontEndApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void loadConfig() throws IOException {
        String directorySeparator = "/";
        String configPath = this.getClass().getPackage().getName().replace(".", directorySeparator) + directorySeparator + "bundle.properties";
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(configPath);
        
        if(null == config){
            config = new Properties();
        }
        config.load(resourceAsStream);
    }

    private AppBuilder getAppBuilder() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

}
